import 'package:get/get.dart';
import '../../../entity/home/content/superior_special_price_entity.dart';
import '../../../utils/Tools.dart';
import '../../http/network.dart';

class DecorationSpecialSuperProductCtr extends GetxController {
  var result = <String, dynamic>{}.obs;
  void fetchData(SuperiorSpecialPriceEntity model) {
    if (model.automatic == '0') {
      specialPriceFetchRecentlyMarketingGoods();
    } else {
      _handleManualMode(model.toJson());
    }
  }

  // 特价组件A1的数据
  Future specialPriceFetchRecentlyMarketingGoods() async {
    try {
      final Map<String, dynamic> data = await Network.getRecentlyMarketingGoods(5, 1, 10);
      result.value = data;
      result.refresh();
    } catch (error) {
      print('特价活动 error $error');
    }
  }

  // 手动模式处理
  Future<void> _handleManualMode(Map<String, dynamic> info) async {
    if (info['sub_entry'] == null || info['sub_entry'].isEmpty) {
      return;
    }
    String marketingId = info['sub_entry'][0]['marketingId'];
    await _getSecKillGoods(marketingId, info);
  }

// 获取活动商品列表的公用方法
  Future<void> _getSecKillGoods(String marketingId, Map<String, dynamic> info) async {
    try {
      Map<String, dynamic>? seckill = await getManualSecKillGoodsList(marketingId, '5');
      if (seckill == null || seckill.isEmpty) {
        return;
      }
      result.value = seckill;
      result.refresh();
    } catch (e) {
      Tools.showToast("获取活动商品列表失败");
    }
  }

  // 手动获取活动商品
  Future getManualSecKillGoodsList(String marketingId, String type) async {
    return await Network.manualQueryMarketingGoods(type, 1, 10, marketingId);
  }
}
